{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br><br><br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Listed Volatility and Variance Derivatives\n",
    "\n",
    "**Wiley Finance (2017)**\n",
    "\n",
    "Dr. Yves J. Hilpisch | The Python Quants GmbH\n",
    "\n",
    "http://tpq.io | [@dyjh](http://twitter.com/dyjh) | http://books.tpq.io\n",
    "\n",
    "<img src=\"http://hilpisch.com/../images/lvvd_cover.png\" alt=\"Derivatives Analytics with Python\" width=\"30%\" align=\"left\" border=\"0\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DX Analytics &mdash; An Overview"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Although Python arguably is a good programming language and ecosystem for financial analytics (see chapter _Introduction_ or chapter 1 of Hilpisch (2018)), dedicated libraries for finance are not that common. This is even more true when it comes to derivatives analytics as a sub-discipline. One exception in this regard is DX Analytics (the \"dx library\"), written by the  author of this book, which has a major focus on advanced derivatives and risk analytics. The central resource to get started with the library is the Web site http://dx-analytics.com."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This chapter gives an overview of the relevant parts of the library for the purposes of the then following case studies. The development of the library is guided by two central principles.\n",
    "\n",
    "* **global valuation approach**: in practice, this approach translates into the non-redundant modeling of all risk factors (e.g. option underlyings like equity indexes) and the valuation of all derivative instruments by a unique, consistent numerical method &mdash; which is Monte Carlo simulation in the case of DX Analytics\n",
    "* **unlimited computing resources**: Monte Carlo simulation is compute and memory intensive and has therefore often been dismissed as an adequate numerical method to implement, for example, front-office analytics libraries; in 2016, the technical infrastructures available to even smaller players in the financial industry have reached performance levels that 10 years ago seemed impossible or at least financially not feasible; in that sense \"unlimited resources\" is not to be understood literally but rather as the guiding principle that hardware and computing resources generally are no longer a bottleneck"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Among others, DX Analytics provides the following features:\n",
    "\n",
    "* **models**: models for risk factors include simple ones like geometric Brownian motion as well as more sophisticated ones like stochastic volatility jump diffusion models\n",
    "* **derivatives**: derivatives models include single risk factor as well as multi risk factor models, both with European and American (Bermudan) exercise\n",
    "* **portfolios**: derivatives portfolios can be arbitraritly complex with multiple, correlated risk factors and multiple, diverse derivative instruments (single risk and multi risk); simulations and valuations are implemented in a fashion that both value and risk aggregations are consistent for each Monte Carlo path\n",
    "\n",
    "The subsequent sections illustrate the use of the library by the means of a simple, yet still realistic example with 2 correlated risk factors and 2 different options."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Modeling Risk Factors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, some necessary imports and in particular the import of the `dx` library."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "import dx\n",
    "import numpy as np\n",
    "np.random.seed(1000)\n",
    "import pandas as pd\n",
    "import datetime as dt\n",
    "from pylab import mpl, plt\n",
    "plt.style.use('seaborn')\n",
    "mpl.rcParams['font.family'] = 'serif'\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Usually, the first step is to define a model for the _risk-neutral discounting_ since all valuations are based on the risk-neutral (or martingale) pricing approach (cf. Björk (2009)). Throughout, we will work with a constant short rate model although DX Analytics also provides deterministic yield curve and stochastic short rate models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = dx.constant_short_rate('r', 0.01)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The next step is to define a _market environment_ containing the parameter specifications needed. Several objects might have different market environments but they might also share market environments. The first risk factor to be modeled is a _geometric Brownian motion_ (Black-Scholes-Merton (1973) model). The following market environment object contains all parameters needed for this model. Comments in the code explain the single elements."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## instantiation of market environment object\n",
    "me_1 = dx.market_environment('me', dt.datetime(2016, 1, 1))\n",
    "## starting value of simulated processes\n",
    "me_1.add_constant('initial_value', 100.)\n",
    "## volatiltiy factor\n",
    "me_1.add_constant('volatility', 0.2)\n",
    "## horizon for simulation\n",
    "me_1.add_constant('final_date', dt.datetime(2016, 6, 30))\n",
    "## currency of instrument\n",
    "me_1.add_constant('currency', 'EUR')\n",
    "## frequency for discretization (here: weekly)\n",
    "me_1.add_constant('frequency', 'W')\n",
    "## number of paths\n",
    "me_1.add_constant('paths', 25000)\n",
    "## short rate model for discount curve\n",
    "me_1.add_curve('discount_curve', r)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Equipped with this object, the model object for the risk factor can be instantiated."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "gbm_1 = dx.geometric_brownian_motion('gbm_1', me_1)\n",
    "gbm_1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `get_instrument_values()` method initiates a Monte Carlo simulation and delivers back the simulated paths &mdash; given the parametrizations from the market environment object &mdash; as a NumPy `ndarray` object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "gbm_1.get_instrument_values()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Via the `time_grid` attribute one can access the date-time information for the time series data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "gbm_1.time_grid[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Combining both arrays to a single pandas ``DataFrame`` object makes plotting straightforward (see the following figure)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "pdf_1 = pd.DataFrame(gbm_1.get_instrument_values(), index=gbm_1.time_grid)\n",
    "pdf_1.iloc[:, :10].plot(legend=False, figsize=(10, 6));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p style=\"font-family: monospace;\">Simulated paths for the risk factor based on geometric Brownian motion"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we define a second risk factor, again based on geometric Brownian motion. We use the market envrionment information from the first risk factor and only overwrite the volatility value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## instantiate new market environment object\n",
    "me_2 = dx.market_environment('me_2', me_1.pricing_date)\n",
    "## add complete environment\n",
    "me_2.add_environment(me_1)\n",
    "## overwrite volatility value\n",
    "me_2.add_constant('volatility', 0.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With this, define the second risk factor as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gbm_2 = dx.geometric_brownian_motion('gbm_2', me_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The plot in the following figure illustrates the higher volatility of the second risk factor graphically."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pdf_2 = pd.DataFrame(gbm_2.get_instrument_values(), index=gbm_2.time_grid)\n",
    "ax = pdf_1.iloc[:, :10].plot(legend=False, figsize=(10, 6), style=11 * ['b-']);\n",
    "pdf_2.iloc[:, :10].plot(legend=False, style=11 * ['r--'], ax=ax);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p style=\"font-family: monospace;\">Simulated paths for the two risk factors; solid lines = low volatility, dashed lines = high volatility"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Modeling Derivatives"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Based on the risk factors, we can define derivatives models for valuation. To this end, we need to add at least one (the `maturity`), in general two (`maturity` and `strike`), parameters to the market environment(s)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "## instantiation of market environment object for option\n",
    "me_opt = dx.market_environment('me_opt', me_1.pricing_date)\n",
    "## add complete market environment\n",
    "me_opt.add_environment(me_1)\n",
    "## add maturity date for option\n",
    "me_opt.add_constant('maturity', dt.datetime(2016, 6, 30))\n",
    "## add strike for option\n",
    "me_opt.add_constant('strike', 110.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first derivative is an _American put option_ on the first risk factor `gbm_1`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "am_put = dx.valuation_mcs_american_single(\n",
    "            name='am_put',  # name of the option as string\n",
    "            underlying=gbm_1,  # the risk factor object\n",
    "            mar_env=me_opt,  # the market environment\n",
    "            payoff_func='np.maximum(strike - instrument_values, 0)')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us calculate a Monte Carlo present value estimate and estimates for the Greeks of the American put."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "am_put.present_value()  # Monte Carlo estimator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "am_put.delta()  # delta of the option"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "am_put.gamma()  # gamma of the option"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "0.5 * am_put.gamma() * am_put.underlying.initial_value ** 2  # dollar gamma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "am_put.vega()  # vega of the option"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "am_put.theta()  # theta of the option "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "am_put.rho()  # rho of the option"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The second derivative is a European call option on the second risk factor `gbm_2`. It has the same strike and maturity as the American put option."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "eur_call = dx.valuation_mcs_european_single(\n",
    "            name='eur_call',\n",
    "            underlying=gbm_2,\n",
    "            mar_env=me_opt,\n",
    "            payoff_func='np.maximum(maturity_value - strike, 0)')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The major statistics for this option are:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "eur_call.present_value()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "eur_call.delta()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "eur_call.gamma()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "0.5 * eur_call.gamma() * eur_call.underlying.initial_value ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "eur_call.vega()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "eur_call.theta()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "eur_call.rho()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that all these values might vary to a smaller or greater extent with the parameters chosen for the Monte Carlo simulation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To conclude this section, let us analyze the European call option in a bit more detail. We want to estimate and collect the Greeks for different strikes. The following code implements the necessary steps."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "k_list = np.arange(80., 120.5, 2.5)\n",
    "pv = []; de = []; ve = []; th = []; rh = []; ga = []\n",
    "for k in k_list:\n",
    "    eur_call.update(strike=k)\n",
    "    pv.append(eur_call.present_value())\n",
    "    de.append(eur_call.delta())\n",
    "    ve.append(eur_call.vega())\n",
    "    th.append(eur_call.theta())\n",
    "    rh.append(eur_call.rho())\n",
    "    ga.append(eur_call.gamma())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following figure shows the results graphically."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "dx.plot_option_stats_full(k_list, pv, de, ga, ve, th, rh)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p style=\"font-family: monospace;\">Greeks of the European call option for different strikes."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Derivatives Portfolios"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The previous sections show how convenient and flexible it is to model single derivatives with DX Analytics. The numerical methods used and the API of the library mimic the working with the Black-Scholes-Merton closed option pricing formula when it comes to Greeks although the derivative itself might be much more complex than a plain vanilla European call or put option (e.g. it might have American exercise and an exotic payoff). However, the area which differentiates DX Analytics most from other derivatives analytics libraries is the global valuation approach for derivatives portfolios. How it works is explained in this section."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Modeling Portfolios"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In a *portfolio context*, we need to add information about the model class(es) to be used by the market environments of the risk factors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "me_1.add_constant('model', 'gbm')\n",
    "me_2.add_constant('model', 'gbm')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To compose a portfolio consisting of the two options, we need to define _derivatives positions_. Note that this step is independent from the risk factor model and option model definitions. We only use the market environment data and some additional information needed (e.g. payoff functions)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "put = dx.derivatives_position(\n",
    "    name='put',  # name as string\n",
    "    quantity=2,  # number of options in the portfolio\n",
    "    underlyings=['gbm_1'],  # the underlying(s) as list object\n",
    "    mar_env=me_opt,  # the market environment object\n",
    "    otype='American single',  # the option type\n",
    "    payoff_func='np.maximum(strike - instrument_values, 0)')  # the payoff\n",
    "\n",
    "call = dx.derivatives_position(\n",
    "    name='call',\n",
    "    quantity=3,\n",
    "    underlyings=['gbm_2'],\n",
    "    mar_env=me_opt,\n",
    "    otype='European single',\n",
    "    payoff_func='np.maximum(maturity_value - strike, 0)')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In a portfolio context, we also need to define the _market_. It consists of the risk factors, the correlation between them, the derivatives positions as well as the valuation environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk_factors = {'gbm_1': me_1, 'gbm_2' : me_2}  # as dictionary\n",
    "correlations = [['gbm_1', 'gbm_2', -0.4]]  # as list\n",
    "positions = {'put' : put, 'call' : call}  # as dictionary"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The *valuation environenment* (technically another market environment) contains all those parameters shared by all derivatives positions. This might imply that certain parameters from the market environments of the derivatives get replaced for the portfolio simulations and valuations. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val_env = dx.market_environment('general', dt.datetime(2016, 1, 1))\n",
    "val_env.add_constant('frequency', 'W')\n",
    "val_env.add_constant('paths', 25000)\n",
    "val_env.add_constant('starting_date', val_env.pricing_date)\n",
    "val_env.add_constant('final_date', val_env.pricing_date)\n",
    "val_env.add_curve('discount_curve', r)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These new objects are needed to instantiate a `portfolio` object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "port = dx.derivatives_portfolio(\n",
    "        name='portfolio',  # name as string\n",
    "        positions=positions,  # derivatives positions\n",
    "        val_env=val_env,  # valuation environment\n",
    "        risk_factors=risk_factors, # relevant risk factors\n",
    "        correlations=correlations,  # correlation between risk factors\n",
    "        parallel=False)  # parallel valuation True/False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Simulation and Valuation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Simulation and valuation are now as straightforward as in the single option case."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "port.get_values()  # get all present values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "port.get_statistics()  # get major statistics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Risk Reports"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A strength of DX Analytics and the global valuation approach  is that one can easily generate consistent risk reports. By this we mean that single parameters are shocked and the effect on the portfolio value is estimated. Think of a larger portfolio containing multiple options on the S&P 500 equity index. By changing the spot value of the index, DX Analytics can estimate in a single step what the impact is on the overall portfolio value (and not only on a single option like in the case of a delta calculation).\n",
    "\n",
    "When calling the `get_port_risk()` method you need to define which parameter shall be shocked. You get back all hypothetical portfolio values and the benchmark value without shock. The following estimates show the \"portfolio deltas\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "deltas, benchvalue = port.get_port_risk(Greek='Delta')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "benchvalue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "deltas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There is a convenience function called `risk_report()` in DX Analytics to nicely print the results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "dx.risk_report(deltas)  # gives the resulting values ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "dx.risk_report(deltas.loc(axis=0)[:, 'value'] - benchvalue,\n",
    "               gross=False)  # ... as net changes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\"Portfolio vegas\" are calculated in the same way."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "vegas, benchvalue = port.get_port_risk(Greek='Vega', step=0.05)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "dx.risk_report(vegas)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "dx.risk_report(vegas.loc(axis=0)[:, 'value'] - benchvalue, gross=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conclusions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This chapter provides a quick start with DX Analytics, a Python-based financial analytics library with a focus on derivatives and risk analytics. The library offers many more features than covered in this brief chapter. It is recommended to check out the main page http://dx-analytics.com and to work through the different parts of the documentation which are all based on executable Jupyter Notebooks.\n",
    "\n",
    "The focus of this chapter is on the basic tool set and a basic understanding of the API to have a good basis for the case studies in the two subsquent chapters. The case studies use DX Analytics to model the VSTOXX volatility index by the *square-root diffusion model* from chapter _Valuing Volatility Derivatives_ as well as the *square-root jump diffusion model* from chapter _Advanced Modeling of the VSTOXX Index. The major goal of the case studies is to analyze how well the two models perform over time in replicating the market quotes of traded VSTOXX options."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br>\n",
    "\n",
    "<a href=\"http://tpq.io\" target=\"_blank\">http://tpq.io</a> | <a href=\"http://twitter.com/dyjh\" target=\"_blank\">@dyjh</a> | <a href=\"mailto:team@tpq.io\">team@tpq.io</a>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
